package com.one.efaimaly.order.presenter;

import android.content.Context;

import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.MaterialItem;
import com.one.common.common.order.MaterialStateItem;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.base.BaseActivity;
import com.one.efaimaly.order.model.OrderModel;
import com.one.efaimaly.order.model.extra.AddMaterialListExtra;
import com.one.efaimaly.order.model.item.OrderDetailItem;
import com.one.efaimaly.order.view.OrderDetailView;

import java.util.ArrayList;
import java.util.List;

/**
 * creator: zhulunjun
 * time:    2018/8/9
 * describe:
 */
public class OrderPresenter extends BaseApiPresenter<OrderDetailView, OrderModel> {
    private DefaultExtra extra;
    private AddMaterialListExtra listExtra;
    private OrderDetailItem orderDetailItem;

    public OrderPresenter(OrderDetailView mView, Context mContext) {
        super(mView, mContext, new OrderModel((BaseActivity) mContext));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        extra = (DefaultExtra) mActivity.getIntent().getSerializableExtra(DefaultExtra.getExtraName());

    }

    /**
     * 获取详情 detail
     */
    private ArrayList<Object> loadDate = new ArrayList<>();

    public void getOrderDetail() {
        if (extra != null) {
            mModel.getOrderDetail(extra.getStr(), new ObserverOnResultListener<OrderDetailItem>() {
                @Override
                public void onResult(OrderDetailItem orderDetailBean) {
                    orderDetailItem = orderDetailBean;
                    if (mView != null) {
                        // 先添加详情
                        loadDate.clear();
                        loadDate.add(orderDetailBean);
                        // 维修项
                        addMaterialData(getAddMaterials(orderDetailBean.getMaintailOrderMaterialBeans()));
                        mView.loadSuccess(loadDate);
                        // 按钮状态
                        mView.setOrderDetail(orderDetailBean);
                    }

                }
            });
        }
    }

    /**
     * 获取有状态的材料列表
     */
    private List<AddMaterialItem> getAddMaterials(List<MaterialItem> data) {
        List<AddMaterialItem> result = new ArrayList<>();
        if (ListUtils.isNotEmpty(data)) {
            for (MaterialItem item : data) {
                if (item.getPrice() > 0) {
                    result.add(new AddMaterialItem(item, false));
                } else {
                    result.add(new AddMaterialItem(item, true));
                }
            }
        }

        return result;
    }

    private void addMaterialData(List<AddMaterialItem> datas) {
        if (mView != null) {
            List<AddMaterialItem> payData = new ArrayList<>();
            List<AddMaterialItem> freeData = new ArrayList<>();
            int total = 0;
            for (AddMaterialItem item : datas) {
                if (item.isFree()) {
                    freeData.add(item);
                } else {
                    payData.add(item);
                }
                total += (item.getNum() * item.getPrice());
            }
            if (payData.size() > 0) {
                loadDate.add(new MaterialStateItem(false));
                for (AddMaterialItem item : payData) {
                    loadDate.add(item);
                }
            }
            if (freeData.size() > 0) {
                loadDate.add(new MaterialStateItem(true));
                for (AddMaterialItem item : freeData) {
                    loadDate.add(item);
                }
            }
            if (total > 0) {
                loadDate.add(new AddMaterialItem(total));
            }

            listExtra = new AddMaterialListExtra(freeData, payData, orderDetailItem.getOrder_id() + "");
        }
    }

    public AddMaterialListExtra getListExtra() {
        return listExtra;
    }

    /**
     * 取消订单
     */
    public void cancel() {
        if (extra != null) {
            mModel.cancel(extra.getStr(), new ObserverOnResultListener<String>() {
                @Override
                public void onResult(String s) {
                    if (mView != null) {
                        mView.finishPage();
                    }
                }
            });
        }
    }


}
